package com.funsport.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.Result;
import com.funsport.entity.PointProduct;
import com.funsport.service.IPointProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * Web端积分商品管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/web/point/product")
@RequiredArgsConstructor
@Tag(name = "Web端积分商品管理")
@PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')")
public class WebPointProductController {
    
    private final IPointProductService pointProductService;
    
    @Operation(summary = "创建商品")
    @PostMapping("/create")
    public Result<Long> createProduct(@RequestBody PointProduct product) {
        try {
            Long productId = pointProductService.createProduct(product);
            return Result.success(productId);
        } catch (Exception e) {
            log.error("创建商品失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "更新商品")
    @PostMapping("/update")
    public Result<Void> updateProduct(@RequestBody PointProduct product) {
        try {
            pointProductService.updateProduct(product);
            return Result.success();
        } catch (Exception e) {
            log.error("更新商品失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取商品列表")
    @GetMapping("/list")
    public Result<Page<PointProduct>> getProductList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String status
    ) {
        try {
            Page<PointProduct> result = pointProductService.getProductList(page, pageSize, category, status);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取商品列表失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取商品详情")
    @GetMapping("/{id}")
    public Result<PointProduct> getProductDetail(@PathVariable Long id) {
        try {
            PointProduct product = pointProductService.getById(id);
            if (product == null) {
                return Result.error("商品不存在");
            }
            return Result.success(product);
        } catch (Exception e) {
            log.error("获取商品详情失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "删除商品")
    @DeleteMapping("/{id}")
    public Result<Void> deleteProduct(@PathVariable Long id) {
        try {
            pointProductService.removeById(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除商品失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "增加库存")
    @PostMapping("/{id}/stock/increase")
    public Result<Void> increaseStock(
            @PathVariable Long id,
            @RequestParam Integer quantity
    ) {
        try {
            pointProductService.increaseStock(id, quantity);
            return Result.success();
        } catch (Exception e) {
            log.error("增加库存失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "更新商品状态")
    @PostMapping("/{id}/status")
    public Result<Void> updateStatus(
            @PathVariable Long id,
            @RequestParam String status
    ) {
        try {
            PointProduct product = pointProductService.getById(id);
            if (product == null) {
                return Result.error("商品不存在");
            }
            product.setStatus(status);
            pointProductService.updateById(product);
            return Result.success();
        } catch (Exception e) {
            log.error("更新商品状态失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
}

